/**
 * 通知
 * @author truexin
 * @author levy 重构，添加权限控制
 * @date   2016/06/20
 */
(function (lf, r) {
    
    var ajax = lf.utils.ajax,
        doAction = lf.bridgeJs.doAction,
        RC = r.createClass;
    
    const CHECK = 'CHECK',
        PUBLISH = 'PUBLISH';
    
    var Panel = React.createClass({
        getInitialState: function () {
            return ({
                numList: [],
                menuList: [],
            })
        },
        componentWillMount: function () {
            var user = this.props.user,
                me = this;
            
            me.resetNum(user);
            lf.appJs.refresh(function (user) {
                me.resetNum(user);
            });
        },
        resetNum: function (user) {
            var me = this;
            //获取数字
            ajax({
                type: 'get',
                data: {
                    staffId: user.id,
                    deptId: user.departmentId
                },
                success: function (data) {
                    me.setState({
                        numList: data.dataList,
                        menuList: me.props.menuList
                    });
                }
            }, lf.api.worktable.num);
        },
        componentDidMount: function () {
            let $listPage = $('.list');
            var me = this;
            $listPage.each(function (i) {
                var $me = $(this);
                $me.off();
                $me.on('tap', function () {
                    var code = me.state.menuList[i].code;
                    
                    if (code == 'APP_NOTICE_REVIEW')
                        doAction('notice-list.html?type=' + CHECK, '待审核');
                    
                    if (code == 'APP_NOTICE_PUBLISH')
                        doAction('notice-list.html?type=' + PUBLISH, '待发布');
                    
                    if (code == 'APP_NOTICE_NEW')
                        doAction('notice-new.html?type=', '最新通知');
                    
                    if (code == 'APP_NOTICE_RULES')
                        doAction('notice-new.html?type=5', '管理制度');
                });
            })
        },
        
        render: function () {
            let item2DArray = lf.utils.to2DArray(this.props.menuList || [], 2);
            let menus = item2DArray.map((array, i) => {
                return (
                    <div key={'array' + i} className="item-row">
                        {
                            array.map(menu => {
                                return <div key={menu.id} className="item list">
                                    <div>
                                        <span className="redNum">{this.getNum(menu.code)}</span>
                                        <p>{menu.name}</p>
                                    </div>
                                </div>
                            })
                        }
                    </div>
                );
            });
            
            return (
                <div className="box">
                    {menus}
                </div>
            );
        },
        getNum(code) {
            let numList = this.state.numList,
                num = '   ';
            if (!numList.length) return num;
            
            for (let i = 0; i < numList.length; i++) {
                let item = numList[i];
                
                
                if (code == item.code) {
                    return num = item.number;
                }
            }
            
        }
    });
    
    /**
     * props:
     *  user
     *  menuList
     */
    var Body = RC({
        getInitialState: function () {
            return ({
                list: []
            })
        },
        componentWillMount: function () {
            this.getList(this.props.user);
            
        },
        getList: function (user) {
            this.setState({
                list: <Panel user={user} menuList={this.props.menuList}/>
            });
        },
        render: function () {
            //TODO 组件化
            if (!this.props.menuList.length) //数据为空
                return <div style={{textAlign: 'center', paddingTop: '1rem'}}>请联系管理员授权</div>
            
            return (
                <div className="notice">
                    {this.state.list}
                </div>
            );
        }
    });
    
    lf.appJs.getUser(function (user) {
        ajax({
            type: 'POST',
            data: {
                staffId: user.id,
                sysMenuId: lf.utils.getUrlParam('menuId')
            }
        }, lf.api.menuList, function (menuList) {
            r.render(
                <Body user={user} menuList={menuList}/>,
                document.getElementById('root'), function () {
                    $('#root').css('background', '#f4f4f4');
                    
                }
            )
        })
    });
    
})(liefeng, React)
